#coding=utf-8
__author__ = 'Administrator'
import time
import urllib2

#build post body data
boundary = '----------%s' % hex(int(time.time() * 1000))
data = []
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_name')
data.append('sbsbsbsb@163.com')
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_email')
data.append('sbsbsbsb@163.com')
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_qq')
data.append('2222222222')
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_phone')
data.append('138552522222')
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_address')
data.append('sbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbs')
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_msg')
data.append('sbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsbsb')
data.append('--%s' % boundary)

path = unicode('E:/1_Meng/投稿.doc','utf8')
fr = open(path,'rb')

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_MAX_FILE_SIZE')
data.append('8129000')
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s";filename="%s"\r\n' %('up[]','TouGao.doc'))
data.append('Content-Type: %s\r\n' % 'application/msword')
data.append(fr.read())
fr.close()
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_siteurl')
data.append('www.chgczz.cn')
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_sitename')
data.append('在线投稿 - 欢迎访问测绘工程杂志')
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_address')
data.append('http://www.chgczz.cn/html/list-6-1.html')
data.append('--%s' % boundary)

data.append('Content-Disposition:form-data;name="%s"\r\n' % 'tg_top_site')
data.append('--%s' % boundary)

url = 'http://61.141.235.120/tg7/zxtg.php'

body = '\r\n'.join(data)

re = True

while re:
    try:
        #request
        request = urllib2.Request(url,data=body)
        #header
        request.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary)
        request.add_header('User-Agent','Mozilla/5.0')
        #open
        response = urllib2.urlopen(request,timeout=10)
        html = str(response.read())
        if "提交成功" in html:
            print("success")
        else:
            print("fail")
        #re = False

    except Exception,e:
        print('post error')
        #re = False
